const baseInfo = require('./sqlStatement/baseInfo.js');

/**
 * 基础资料系统获取老版商品信息接口
 */

const getProductInfo = async (ctx, next) => {
    try {
        const body = ctx.query;
        const result = await baseInfo.getProdutInfo(body);
        const result1 = await baseInfo.getProductTotal(body);
        ctx.body = {
            data: result.recordset,
            total: result1.recordset[0].TOTAL
        };
    } catch (err) {
        ctx.body = err;
    }
}
/**
 * 基础资料系统添加商品信息接口
 */
const getAddData = async (ctx, next) => {
    try {
        const body = ctx.request.body;
        const result = await baseInfo.getAddData(body);
        ctx.body = result.recordset;
    } catch (err) {
        ctx.body = err;
    }
}
/**
 * 基础资料系统获取新版商品信息接口
 */
const getProduct = async (ctx, next) => {
    try {
        const body = ctx.query;
        const result = await baseInfo.getProduct(body);
        const result1 = await baseInfo.getTotal(body);
        ctx.body = {
            data: result.recordset,
            total: result1.recordset[0].TOTAL
        };
    } catch (err) {
        ctx.body = err;
    }
}
/**
 * 更新基础资料系统商品信息接口
 */
const updateProduct = async (ctx, next) => {
    try {
        const body = ctx.request.body;
        const result = await baseInfo.updateProduct(body[0]);
        ctx.body = result;
    } catch (err) {
        ctx.body = err;
    }
}
/**
 * 商家编码用Excel批量导入接口
 */
const setExcel = async (ctx, next) => {
    try {
        const body = ctx.request.body;
        const result = await baseInfo.insertExcel(body);
        ctx.body = result;
    } catch (err) {
        ctx.body = err;
    }
}
/**
 * 基础资料系统获取商家和速拓编码接口
 */
const getCoding = async (ctx, next) => {
    try {
        const body = ctx.query;
        const result = await baseInfo.getCoding(body);
        const result1 = await baseInfo.getCodingTotal();
        ctx.body = {
            data: result.recordset,
            total: result1.recordset[0].TOTAL
        };
    } catch (err) {
        ctx.body = err;
    }
}
const getCarriageRack = async (ctx, next) => {
    try {
        const body = ctx.query;
        const result = await baseInfo.getCarriageRack(body);
        const result1 = await baseInfo.getCodingTotal();
        ctx.body = {
            data: result.recordset,
            total: result1.recordset[0].TOTAL
        };
    } catch (err) {
        ctx.body = err;
    }
}


module.exports = {
    'GET /getProductInfo': getProductInfo,
    'GET /getProduct': getProduct,
    'GET /getCoding': getCoding,
    'GET /getCarriageRack': getCarriageRack,
    'PUT /updateProduct': updateProduct,
    'POST /getAddData': getAddData,
    'POST /setExcel': setExcel
}